package com.bbznb.common;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;        // 状态码
    private String message;      // 状态信息
    private T data;              // 响应数据

    // 私有构造方法，防止外部直接创建实例
    private Result() {}
    // 静态工厂方法，用于创建实例
    public static <T> Result<T> build(Integer code, String message, T data)  {
        return new Result<T>(code, message, data );
    }
    /**
     * 成功响应构造方法（带数据）
     */
    private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功响应构造方法（不带数据）
     */
    private Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 失败响应构造方法
     */
    private Result(Integer code, String message, Throwable e) {
        this.code = code;
        this.message = message;
        if (e != null) {
            this.message += " | " + e.getMessage();
        }
    }

    // 成功响应静态工厂方法
    public static <T> Result<T> success() {
        return new Result<>(200, "操作成功");
    }

    public static <T> Result<T> success(String message) {
        return new Result<>(200, message);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, "操作成功", data);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(200, message, data);
    }

    // 失败响应静态工厂方法
    public static <T> Result<T> error() {
        return new Result<>(500, "操作失败");
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(500, message);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message);
    }

    public static <T> Result<T> error(Throwable e) {
        return new Result<>(500, "操作失败", e);
    }

    public static <T> Result<T> error(Integer code, String message, Throwable e) {
        return new Result<>(code, message, e);
    }

    // Getter方法
    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}